package pers.amos.eshop.advance.product.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pers.amos.eshop.advance.product.common.DataTypeConstants;
import pers.amos.eshop.advance.product.common.EventTypeConstants;
import pers.amos.eshop.advance.product.mapper.ProductSpecificationMapper;
import pers.amos.eshop.advance.product.model.ProductSpecification;
import pers.amos.eshop.advance.product.mq.RabbitMQSender;
import pers.amos.eshop.advance.product.service.ProductSpecificationService;


@Service
public class ProductSpecificationServiceImpl implements ProductSpecificationService {

    @Autowired
    private ProductSpecificationMapper productSpecificationMapper;

    @Autowired
    private RabbitMQSender rabbitMqSender;

    @Override
    public void add(ProductSpecification productSpecification, String operationType) {
        productSpecificationMapper.add(productSpecification);

        String sendMessage = RabbitMQSender.buildProductDimData(EventTypeConstants.ADD_EVENT_TYPE,
                DataTypeConstants.PRODUCT_SPECIFICATION_DATA_TYPE,
                productSpecification.getId(), productSpecification.getProductId());

        String queue = rabbitMqSender.decideSendMegQueue(operationType);

        rabbitMqSender.send(queue, sendMessage);
    }

    @Override
    public void update(ProductSpecification productSpecification, String operationType) {
        productSpecificationMapper.update(productSpecification);

        String sendMessage = RabbitMQSender.buildProductDimData(EventTypeConstants.UPDATE_EVENT_TYPE,
                DataTypeConstants.PRODUCT_SPECIFICATION_DATA_TYPE,
                productSpecification.getId(), productSpecification.getProductId());

        String queue = rabbitMqSender.decideSendMegQueue(operationType);

        rabbitMqSender.send(queue, sendMessage);
    }

    @Override
    public void delete(Long id, String operationType) {
        productSpecificationMapper.delete(id);

        String sendMessage = RabbitMQSender.buildSendData(EventTypeConstants.DELETE_EVENT_TYPE,
                DataTypeConstants.PRODUCT_SPECIFICATION_DATA_TYPE, id);

        String queue = rabbitMqSender.decideSendMegQueue(operationType);

        rabbitMqSender.send(queue, sendMessage);
    }

    @Override
    public ProductSpecification findById(Long id) {
        return productSpecificationMapper.findById(id);
    }

    @Override
    public ProductSpecification findByProductId(Long productId) {
        return productSpecificationMapper.findByProductId(productId);
    }

}
